package morncloud.com.notes.database;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import java.util.ArrayList;

import morncloud.com.notes.beans.Constants;

/**
 * Created by majoy on 2015/1/5.
 */
public class DAO {
    // define the singleton
    public static DAO mDAO;
    /**
     * define the lock
     */
    public final static Object LOCK = new Object();
    public Context mContext;

    /**
     * define the Constructor
     *
     * @param context
     */
    public DAO(Context context) {
        mContext = context;
    }

    /**
     * singleton
     *
     * @return
     */
    public static DAO getInstance(Context context) {
        synchronized (LOCK) {
            if (mDAO == null) {
                mDAO = new DAO(context);
            }
            return mDAO;
        }
    }

    /**
     * define the CRUD
     *
     * @param table
     * @param nulCloumnHack
     * @param values
     * @return
     */

    public int insert(String table, String nulCloumnHack, ContentValues values) {
        synchronized (LOCK){
            ContentResolver cr = mContext.getContentResolver();
            return cr.insert(Constants.URI_NOTE,values)==null?0:1;
        }
    }

    /**
     * {@link #insert(String, String, android.content.ContentValues)}
     *
     * @param table
     * @param values
     * @param whereClause
     * @param whereArgs
     * @return
     */

    public int update(String table, ContentValues values, String whereClause, String[] whereArgs) {
        synchronized (LOCK){
            NoteDataBaseOpenHelper openHelper = new NoteDataBaseOpenHelper(mContext);
            ContentResolver cr = mContext.getContentResolver();
           return cr.update(Constants.URI_NOTE,values,whereClause,whereArgs);
        }
    }

    /**
     * define the single query
     *
     * @param cloumns
     * @param selection
     * @param selectionArgs
     * @param groupBy
     * @param having
     * @param oderBy
     * @return
     */

    public NoteDataUnit query(String[] cloumns, String selection, String[] selectionArgs, String groupBy, String having, String oderBy) {
        NoteDataUnit unit = null;
        Cursor cursor = null;
        try {
            ContentResolver cr = mContext.getContentResolver();
            cursor = cr.query(Constants.URI_NOTE,cloumns,selection,selectionArgs,oderBy);
            if (cursor.moveToFirst()) {
                unit = new NoteDataUnit();
                unit.mId = cursor.getString(cursor.getColumnIndex(Provider.NoteDataCloumns._ID));
                unit.mData = cursor.getString(cursor.getColumnIndex(Provider.NoteDataCloumns.DATA));
                unit.mCreate = cursor.getString(cursor.getColumnIndex(Provider.NoteDataCloumns.CREATE_TIME));
                unit.mAlert = cursor.getString(cursor.getColumnIndex(Provider.NoteDataCloumns.ALERT_TIME));
                unit.mMotified = cursor.getString(cursor.getColumnIndex(Provider.NoteDataCloumns.MOTIFIED_TIME));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return unit;
    }

    public void bultInsert() {

    }

    public int insertToNotes(String content) {
        Log.e("print", content);
        ContentValues values = new ContentValues();
        values.put(Provider.NoteDataCloumns.DATA, content);
        int i = insert(Provider.TABLE_NOTES, null, values);
        return i;
    }

    public Cursor getAllNotes() {
        synchronized (LOCK){
            Cursor cursor = null;
            try {
                ContentResolver cr = mContext.getContentResolver();
                cursor = cr.query(Constants.URI_NOTE, null, null, null, null, null);
            } catch (Exception e) {
                e.printStackTrace();
                if (cursor != null) {
                    cursor.close();
                }
            } finally {
            }
            return cursor;

        }
    }

    /**
     * @param id the _id
     * @return the NotaDataUnit ,the data involved
     */

    public NoteDataUnit queryWithId(long id) {
        NoteDataUnit unit = null;
        String selection = Provider.NoteDataCloumns._ID + " = ?";
        String selectionArg = Long.toString(id);
        unit = query(null, selection, new String[]{selectionArg}, null, null, null);
        return unit;
    }

    /**
     * we get data invovled
     * @param unit
     * @return rowid
     */

    public int updateWithId(NoteDataUnit unit) {
        synchronized (LOCK) {
            String where = Provider.NoteDataCloumns._ID + " = ?";
            String whereArgs[] = new String[]{unit.mId};
            ContentValues values = new ContentValues();
            values.put(Provider.NoteDataCloumns.DATA, unit.mData);
            if (unit.mAlert != null) {
                values.put(Provider.NoteDataCloumns.ALERT_TIME, unit.mAlert);
            }
            ContentResolver cr = mContext.getContentResolver();
            return cr.update(Constants.URI_NOTE, values, where, whereArgs);
        }
    }

    /**
     * will delete the notes with _id
     *
     * @param arrayList
     * @return
     */

    public int deleteNotesWithId(ArrayList<Long> arrayList) {
        synchronized (LOCK) {
            Log.e("print","deleteNotesWithId");

            String selection = Provider.NoteDataCloumns._ID + " =?";
            String selectionArg[] = new String[arrayList.size()];
            int i = 0;
            ContentResolver cr = mContext.getContentResolver();
            for ( i = 0; i < arrayList.size(); i++) {
                selectionArg[i] = Long.toString(arrayList.get(i));
                cr.delete(Constants.URI_NOTE,selection,new String[]{selectionArg[i]});
            }
            return  i;
        }
    }

}
